package com.qf.dao.impl;

import com.qf.dao.CartDao;
import com.qf.domain.Cart;
import com.qf.domain.User;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public Cart find(Integer uid, int goods_id) {

        String sql=" SELECT `id`,`pid`,`num`,`money` FROM `tb_cart` WHERE `id`= ? AND `pid`= ? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Cart.class), uid,goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void add(Cart cart) {
        String sql=" INSERT INTO `tb_cart`(`id`,`pid`,`num`,`money`) VALUES (?,?,?,?) ";
        Object[] params={cart.getId(),cart.getPid(),cart.getNum(),cart.getMoney()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void modify(Cart cart) {
        String sql=" UPDATE `tb_cart` SET `num`=? ,`money`=? WHERE `id`=? AND `pid`=? ";
        Object[] params={cart.getNum(),cart.getMoney(),cart.getId(),cart.getPid()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Cart> findByUserId(Integer uid) {
        String sql=" SELECT `id`,`pid`,`num`,`money` FROM `tb_cart` WHERE `id`= ?  ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Cart.class), uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void remove(Integer uid, int goods_id) {
        String sql=" DELETE FROM `tb_cart` WHERE `id`=? AND `pid`=? ";
        try {
            queryRunner.update(sql,uid,goods_id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAll(Integer uid) {
         QueryRunner queryRunnery1=new QueryRunner();
         Connection connection=DataSourceUtil.getConnection();
        String sql=" DELETE FROM `tb_cart` WHERE `id`=? ";
        try {
            queryRunnery1.update(connection,sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtil.closeAll(null,null,connection);
        }
    }

    @Override
    public void removeByUId(int uid) {
        String sql=" DELETE FROM `tb_cart` WHERE `id`=? ";
        try {
            queryRunner.update(sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
